﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NumberRecognition.Presenters;
using NumberRecognition.Interfaces;

namespace NumberRecognition
{
    public partial class MainForm : Form, IMainFormPresenter
    {
        private static Brush pen = Brushes.Black;
        private static Graphics grapics;
        private static Graphics pictureBox;
        private static Bitmap image;
        private static bool draw = false;
        private static int bold = 10;
        private MainFormPresenter presenter;

        public MainForm()
        {
            InitializeComponent();
            presenter = new MainFormPresenter(this, (float)LearningSpeedUpDown.Value, (int)NumberOfEpochsUpDown.Value);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.White;
            image = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            presenter.SetLearningElementEnabled(false);
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Cross;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            draw = true;            
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            draw = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            grapics = Graphics.FromImage(image);//pictureBox1.CreateGraphics();
            pictureBox = pictureBox1.CreateGraphics();
            int x1 = e.X;
            int y1 = e.Y;

            if (draw)
            {
                grapics.FillEllipse(pen, x1, y1, bold, bold);
                pictureBox.DrawImageUnscaled(image, 0, 0);
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            grapics.Clear(Color.White);
            pictureBox.Clear(Color.White);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                presenter.SetPath(folderBrowserDialog1.SelectedPath);                

            }
        }

        public bool LearningSpeedEnabled
        {
            set { LearningSpeedUpDown.Enabled = value; }
        }

        public bool NumberOfEpochsEnabled
        {
            set { NumberOfEpochsUpDown.Enabled = value; }
        }

        public bool StartLearningButton
        {
            set { StartLearning.Enabled = value; }
        }

        public void ShowMessage(string message)
        {
            MessageBox.Show(message);
        }

        public string Result
        {
            set { ResultLabel.Text = value; }
        }

        private void StartLearning_Click(object sender, EventArgs e)
        {
            presenter.StartLearning();
        }

        private void RecognizeButton_Click(object sender, EventArgs e)
        {            
            presenter.TryToRecognize(image);            
        }
    }
}
